#!/usr/bin/env perl

# Simple Chromium launcher without support for Pepper Flash
#
# Some rudimentary support for user flags is provided via a @CHROMIUM_NAME@-flags.conf
# config file placed in $HOME/.config/ (or $XDG_CONFIG_HOME). Arguments are
# split on whitespace and shell quoting rules apply but no further parsing is
# performed. In case of improper quoting anywhere in the file, a fatal error is
# raised. Lines starting with a hash symbol (#) are skipped.

use strict;
use warnings;

use Cwd qw( abs_path );
use JSON::PP qw( decode_json );
use File::BaseDir qw( config_home );
use Text::ParseWords qw ( shellwords );


sub get_user_flags {
	my $conf_path = config_home '@CHROMIUM_NAME@-flags.conf';
	open my $conf, '<', $conf_path or return;

	my @lines = grep {!/^(\s*#|\s*$)/} map { chomp; $_ } <$conf>;
	return if not @lines;

	my @user_flags = shellwords @lines;

	unless (@user_flags) {
		system '@LAUNCHER_ERRMSG@',
			'Unable to parse user flags',
			"Please check $conf_path for errors (e.g. mismatched quotes).\n\n" .
			"The launcher will now exit.";
		exit 1;
	}

	return @user_flags;
}

$ENV{CHROME_WRAPPER} = abs_path($0);
$ENV{CHROME_DESKTOP} = '@CHROMIUM_NAME@.desktop';

exec '@CHROMIUM_BIN@', get_user_flags, @ARGV;
